<?php
require_once 'rabbitlib.php';
use PhpAmqpLib\Connection\AMQPStreamConnection;
use PhpAmqpLib\Exchange\AMQPExchangeType;
use PhpAmqpLib\Message\AMQPMessage;
$exchange = 'router';
$queue = 'msgs1';

$host = '127.0.0.1';
$port = 5672;
$user = 'guest';
$pass = 'guest';
$vhost = '/';
try {
    $connection = new AMQPStreamConnection($host, $port, $user, $pass, $vhost);
}catch (Exception $e){
    echo $e->getMessage();
}
$channel = $connection->channel();


/*
    name: $queue
    passive: false
    durable: true // the queue will survive server restarts
    exclusive: false // the queue can be accessed in other channels
    auto_delete: false //the queue won't be deleted once the channel is closed.
*/
$channel->queue_declare($queue, false, true, false, false);

/*
    name: $exchange
    type: direct
    passive: false
    durable: true // the exchange will survive server restarts
    auto_delete: false //the exchange won't be deleted once the channel is closed.
*/

$channel->exchange_declare($exchange, AMQPExchangeType::DIRECT, false, true, false);


$channel->queue_bind($queue, $exchange);

$messageBody = 'testsetatat';
$message = new AMQPMessage($messageBody, array('content_type' => 'text/plain', 'delivery_mode' => AMQPMessage::DELIVERY_MODE_PERSISTENT));
$i = 0;
while($i<10){

    $i++;
    try {
        $res = $channel->basic_publish($message, $exchange);
    }catch (Exception $e){
        echo $e->getMessage();
    }
    print($res);
}

$channel->close();
$connection->close();

